<template>
  <div class="dialog">
    <!-- 办理弹框 -->
		<el-dialog
			:title="title"
			:visible="dialogVisiblehandle"
			:close-on-click-modal="false"
			width="1500px"
			class="bldialog"
      @close="closeDialog"
			v-dialogDrag="true"
		>
      <el-form class="handleform" disabled :model="handleform" size="small" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="单位名称">
              <el-input
                v-model="handleform.organizationName"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合同编号">
              <el-input v-if="handleform.agreementNumber" v-model="handleform.agreementNumber" disabled></el-input>
              <el-select
                v-else
                v-model="handleform.agreementNumber"
                placeholder="请选择"
                style="width: 100%"
                clearable>
                <el-option
                  :label="item.agreementNumber"
                  :value="item.agreementNumber"
                  v-for="(item,index) in agreementList"
                  :key="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人">
              <el-input
                v-model="filterContact"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="联系电话">
              <el-input
                v-model="filterMobile"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检测周期(天)">
              <el-input-number
                v-model="handleform.monitoringCycle"
                disabled
                :min="1"
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检测启用状态">
              <el-select
                v-model="handleform.monitoringState"
                disabled
                style="width: 100%">
                <el-option
                  v-for="item in dict.type.status_disable"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="单位地址">
              <el-input
                v-model="handleform.organizationAddress"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="佩戴开始日期">
              <el-date-picker
                style="width:100%;"
                v-model="handleform.wearingBeginTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="date"
                :picker-options="pickerOptionsHandleF"
                placeholder="请选择"
                clearable>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="佩戴结束日期">
              <el-date-picker
                style="width:100%;"
                v-model="handleform.wearingEndTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="date"
                :picker-options="pickerOptionsHandleS"
                placeholder="请选择"
                clearable>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
        </el-row>
      </el-form>

      <el-table class="handletabel"
        ref="tableData"
        :class="'handletabelaudit'"
        v-loading="loadingHandle"
        :data="handleformLists"
        :row-class-name="tableRowClassName"
        :cell-class-name="tableCellClassName"
				height="400"
        >
        <el-table-column label="剂量计编号" align="center" prop="dosageNumber" width="100"/>
        <el-table-column label="姓名" align="center" prop="showTypeName" width="120" :show-overflow-tooltip="true"/>
        <el-table-column label="剂量元件码" align="center" prop="slidesNumber" width="100"/>
        <el-table-column label="委托单位" align="center" prop="entrustOrganizationName" :show-overflow-tooltip="true"/>
        <el-table-column label="佩戴起止日期" align="center" width="200">
          <template slot-scope="{row}">
            <span>
              {{ parseTime(row.wearingBeginTime, "{y}/{m}/{d}") }} - {{ parseTime(row.wearingEndTime, "{y}/{m}/{d}") }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="孔一读出值/系数" align="center" width="150">
          <template slot-scope="{row}">
            {{ `${row.holeOne ? row.holeOne : ''} / ${row.holeOneCoefficient ? row.holeOneCoefficient : ''}` }}
          </template>
        </el-table-column>
        <el-table-column label="孔二读出值/系数" align="center" width="150" >
          <template slot-scope="{row}">
            {{ `${row.holeTwo ? row.holeTwo : ''} / ${row.holeTwoCoefficient ? row.holeTwoCoefficient : ''}` }}
          </template>
        </el-table-column>
        <el-table-column label="接样日期" align="center" prop="samplingDate" width="120">
          <template slot-scope="{row}">
            {{ parseTime(row.samplingDate, "{y}/{m}/{d}") }}
          </template>
        </el-table-column>
        <el-table-column label="剂量当量" align="center" prop="dosageValue" width="80">
          <template slot-scope="{ row }">
            {{ row.dosageValue }}
          </template>
        </el-table-column>
        <el-table-column label="检测时间" align="center" prop="detectionTime" width="150">
          <template slot-scope="{row}">
            {{ parseTime(row.detectionTime, "{y}/{m}/{d}") }}
          </template>
        </el-table-column>
				<el-table-column
					align="center"
					label="操作"
					width="100"
					v-if="status == '2'">
					<template slot-scope="{row}">
						<el-button
							@click.native.prevent="handleDelete(row)"
							type="text"
							class="ovbutton"
							style="color: #f32d2d">
							删除
						</el-button>
					</template>
				</el-table-column>
      </el-table>

      <pagination
        class="handle"
        style="margin-bottom: 5px"
        v-show="totalHandle>0"
        :total="totalHandle"
        :page.sync="queryParamsHandle.pageNum"
        :limit.sync="queryParamsHandle.pageSize"
        @pagination="getListHandle"
      />
			<div slot="footer">
				<el-button @click="closeDialog">关 闭</el-button>
			</div>
		</el-dialog>
  </div>
</template>

<script>
import {
  slidesDetectionList, detectionRemove
} from "@/api/slides/inspect";
import {personalDosimeterDetail} from "@/api/slides/task";
import {getConfiglargeDose} from "@/api/slides/abnormal";
import { getToken } from "@/utils/auth";
export default {
  name: 'inspectDialog',
  dicts: ['status_disable'],
  props: {
    open: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: "检测登记"
    },
    rowData: {
      type: Object,
      default: () => {}
    },
    status: [Number, String]
  },
  computed: {
    dialogVisiblehandle() {
      return this.open
    },
    filterContact() {
      return this.handleform.dosageContactsName || this.handleform.contactsName
    },
    filterMobile() {
      return this.handleform.dosageContactsMobile || this.handleform.contactsMobile
    },
  },
  watch: {
    rowData: {
      handler(val) {
        // let {entrustOrganizationName, } = val || {}
        this.rowDatas = val;
        this.personalDosimeterDetails()
      },
      deep: true,
      immediate: true
    }
  },
  data() {
    return {
      // dialogVisiblehandle: this.dialogVisiblehandle,
      handleform: {},
			// 合同下拉框
			agreementList: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 参数
        data: {
          equipmentId: null,
        },
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/basic/slide/detection/importData",
      },
      handleformLists: [],
			loadingHandle: false,
      totalHandle: 0,
			// 大剂量标准值
			largeDose: null,
      // 办理的查询参数
      queryParamsHandle: {
        taskPersonalDosimeterId: this.rowDatas?.taskPersonalDosimeterId,
        pageNum: 1,
        pageSize: 10,
      },
      tasksState: [
        {
          label: '保存中',
          value: 0
        },
        {
          label: '审核中',
          value: 1
        },
        {
          label: '已通过',
          value: 2
        },
        {
          label: '已拒绝',
          value: 3
        },
      ],
      typeLists: [
        {
          label: '待绑定',
          value: 0
        },
        {
          label: '待检测',
          value: 1
        },
        {
          label: '待计算',
          value: 2
        },
        {
          label: '异常',
          value: 3
        },
        {
          label: '待出报告',
          value: 4
        },
        {
          label: '已出报告',
          value: 5
        }
      ],
      dosageTypes: [
        {
          label: '正常',
          value: 0
        },
        {
          label: '铅衣内',
          value: 1
        },
        {
          label: '铅衣外',
          value: 2
        },
        {
          label: '本底',
          value: 3
        },
      ],
      pickerOptionsHandleF: {
        disabledDate: (time) => {
          return time.getTime() > new Date(this.handleform.wearingEndTime).getTime();
        },
      },
      pickerOptionsHandleS: {
        disabledDate: (time) => {
          return time.getTime() < new Date(this.handleform.wearingBeginTime).getTime();
        },
      },
    }
  },
  created() {
    this.configlargeDose()
    this.getListHandle()
  },
  methods: {
    // 分页获取人员（根据row.taskPersonalDosimeterId）
		getListHandle() {
			this.queryParamsHandle.dosimeterId = this.rowDatas?.taskPersonalDosimeterId;
      this.loadingHandle = true;
			slidesDetectionList(this.queryParamsHandle).then((response) => {
				this.handleformLists = response.rows || [];
				if (this.handleformLists.length != 0) {
					this.handleformLists.forEach(item => {
						item.showTypeName = item.personnelName + (item.dosageType == '1' ? '(内)' : item.dosageType == '2' ? '(外)' : '');
						item.originName = item.personnelName;
						item.isEmptyTrackingNumber = !item.sendTrackingNumber;
					})
				}
				this.totalHandle = response.total;
				this.loadingHandle = false;
			}).catch(response => {
				this.loadingHandle = false;
			});
		},
    // 列表标红
    tableRowClassName({ row, rowIndex }) {
      if ((this.largeDose && row.dosageValue > this.largeDose) || row.exceptional) {
        return "warning-row";
      }
      return "";
    },
    // 给单元格绑定横向和竖向的index，这样就能确定是哪一个单元格
    tableCellClassName({row, column, rowIndex, columnIndex}){
      row.index = rowIndex;
      column.index = columnIndex;
    },
    closeDialog() {
      this.$emit('close', false)
    },
    getLabel(selectData, selectValue) {
      console.log(selectData, selectValue)
      return selectData.find(item => item.value == selectValue)?.label || ''
    },
		// 获取大剂量标准值
		configlargeDose() {
      getConfiglargeDose().then(response => {
				this.largeDose = response.msg;
			})
		},
    // 获取下拉框
    getAgreementListWithTaskDosimeters() {
      getAgreementListWithTaskDosimeter({'entrustOrganizationId': response.data.entrustOrganizationId}).then((response) => {
        this.agreementList = response.data;
      });
    },
    //
    personalDosimeterDetails() {
      personalDosimeterDetail(this.rowDatas?.taskPersonalDosimeterId).then((response) => {
				if(response.data) {
					if (response.data.monitoringState != null) {
						response.data.monitoringState = response.data.monitoringState.toString();
					}
					this.handleform = response.data;
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm(`是否确认删除该检测结果？`)
        .then(() => {
          // this.loading = true;
          return detectionRemove(row.taskPersonalResultId);
        })
        .then(() => {
          // this.loading = false;
          this.$modal.msgSuccess("删除成功");
          this.getListHandle();
        })
        .finally(() => {
          // this.loading = false;
        });
    },
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-table .warning-row {
	color: red;
}
::v-deep .el-button.ovbutton {
	padding-block: 0;
}
</style>
